<?php

/*
 * SETUP CONFIG
 * 
 * Edit the following variables to fit your needs
 * 
 * Also don't forget to configure the sql settings in "controls/sql_setup.php"
 */

$admin_first_name = 'Admin';
$admin_second_name = 'TheBoss';
$admin_password = 'keines';
$admin_email = 'admin@todos';

$install_now = false; //set this true and run this php on your server to install the Todo Server

/*
 * SETUP CODE
 */

define('ROOT_DIR', dirname(__FILE__).'/');

if($install_now){

	require_once ROOT_DIR.'controls/sql_setup.php';
	
	$sql = 'SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
	
	CREATE TABLE IF NOT EXISTS `'.TABLE_PREFIX.'lists` (
	  `id` int(11) NOT NULL auto_increment,
	  `name` varchar(200) collate utf8_unicode_ci NOT NULL,
	  `description` text collate utf8_unicode_ci NOT NULL,
	  `owner` int(11) NOT NULL,
	  `creation_date` int(11) NOT NULL,
	  PRIMARY KEY  (`id`)
	) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
	
	CREATE TABLE IF NOT EXISTS `'.TABLE_PREFIX.'messages` (
	  `id` int(11) NOT NULL auto_increment,
	  `linked_to_message` int(11) NOT NULL,
	  `linked_to_list` int(11) NOT NULL,
	  `linked_to_todo` int(11) NOT NULL,
	  `linked_to_user` int(11) NOT NULL,
	  `author` int(11) NOT NULL,
	  `title` varchar(200) collate utf8_unicode_ci NOT NULL,
	  `text` text collate utf8_unicode_ci NOT NULL,
	  `date` int(11) NOT NULL,
	  PRIMARY KEY  (`id`)
	) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
	
	CREATE TABLE IF NOT EXISTS `'.TABLE_PREFIX.'share_list_user` (
	  `list` int(11) NOT NULL,
	  `user` int(11) NOT NULL,
	  PRIMARY KEY  (`list`,`user`)
	) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
	
	CREATE TABLE IF NOT EXISTS `'.TABLE_PREFIX.'todos` (
	  `id` int(11) NOT NULL auto_increment,
	  `title` varchar(200) collate utf8_unicode_ci NOT NULL,
	  `description` text collate utf8_unicode_ci NOT NULL,
	  `creation_date` int(11) NOT NULL,
	  `target_date` int(11) NOT NULL,
	  `projected_hours` float NOT NULL,
	  `creator` int(11) NOT NULL,
	  `status` varchar(30) collate utf8_unicode_ci NOT NULL,
	  PRIMARY KEY  (`id`)
	) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
	
	CREATE TABLE IF NOT EXISTS `'.TABLE_PREFIX.'todo_list` (
	  `todo` int(11) NOT NULL,
	  `list` int(11) NOT NULL,
	  PRIMARY KEY  (`todo`,`list`)
	) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
	
	CREATE TABLE IF NOT EXISTS `'.TABLE_PREFIX.'todo_user` (
	  `todo` int(11) NOT NULL,
	  `user` int(11) NOT NULL,
	  `role` varchar(100) collate utf8_unicode_ci NOT NULL,
	  PRIMARY KEY  (`todo`,`user`)
	) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
	
	CREATE TABLE IF NOT EXISTS `'.TABLE_PREFIX.'users` (
	  `id` int(11) NOT NULL auto_increment,
	  `login_name` varchar(100) collate utf8_unicode_ci NOT NULL,
	  `password` varchar(100) collate utf8_unicode_ci NOT NULL,
	  `first_name` varchar(100) collate utf8_unicode_ci NOT NULL,
	  `last_name` varchar(100) collate utf8_unicode_ci NOT NULL,
	  `email` varchar(100) collate utf8_unicode_ci NOT NULL,
	  `online` int(1) NOT NULL,
	  `group` varchar(50) collate utf8_unicode_ci NOT NULL,
	  PRIMARY KEY  (`id`),
	  UNIQUE KEY `login_name` (`login_name`)
	) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=2 ;
	
	INSERT INTO `td_users` (`id`, `login_name`, `password`, `first_name`, `last_name`, `email`, `online`, `group`) VALUES
	(1, \'admin\', \''.md5($admin_password).'\', \''.$admin_first_name.'\', \''.$admin_second_name.'\', \''.$admin_email.'\', 0, \'admin\');
	
	CREATE TABLE IF NOT EXISTS `'.TABLE_PREFIX.'workintervals` (
	  `id` int(11) NOT NULL auto_increment,
	  `user` int(11) NOT NULL,
	  `todo` int(11) NOT NULL,
	  `start_date` int(11) NOT NULL,
	  `end_date` int(11) NOT NULL,
	  PRIMARY KEY  (`id`)
	) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;';
	
	if(mysql_query($sql)){
		$msg = 'The Todo Server has been set up successfully.<br/>You should delete this file from your server now.';
	} else {
		$msg = 'An error occured while creating the database tables.';	
	}
	
	echo '
		<html>
			<head><title>Todo Server Setup</title></head>
			<body>
				'.$msg.'
			</body>
		</html>
	';
	
} else {
	echo '
		<html>
			<head><title>Todo Server Setup</title></head>
			<body>
				If you see this you haven\'t configured the install file yet.<br/>
				To do this open this file with the texteditor of your choice and follow the instruction.
			</body>
		</html>
	';
}

?>